package com.yhp.vx.controller;

import com.yhp.bean.*;
import com.yhp.mvc.ResponseBody;
import com.yhp.mvc.ResponseView;
import com.yhp.service.impl.ExpressService;
import com.yhp.util.DateFormatUtil;
import com.yhp.util.GsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QRcodeController {
    @ResponseView("/vx/createQrcode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response){
        String type = request.getParameter("type");
        String code = request.getParameter("code");
        Object user = request.getSession().getAttribute("user");
        int info = (int) request.getSession().getAttribute("userInfo");
        String userPhone = null;
        String qrcodeContent = null;
        if(info == 0){
            Courier c = (Courier) user;
            userPhone = c.getPhone();

        }else if(info == 1){
            User u = (User) user;
            userPhone = u.getPhone();
        }
        System.out.println(userPhone);

        if("express".equals(type)){
            //取件二维码
            qrcodeContent = "express_"+code;
        }else if("user".equals(type)){
            //手机号
            qrcodeContent = "userPhone_"+userPhone;
        }else {
            return null;
        }
        request.getSession().setAttribute("qrcode",qrcodeContent);

        return "/personQRcode.html";
    }


    @ResponseBody("/vx/qrcode.do")
    public String getQrcode(HttpServletRequest request, HttpServletResponse response){
        String qrcode = (String) request.getSession().getAttribute("qrcode");
        Message msg = new Message();
        if(qrcode != null){
            msg.setStatus(0);
            msg.setResult(qrcode);
        }else {
            msg.setStatus(-1);
            msg.setResult("取件码获取出错");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/vx/updateStatus.do")
    public String updateStatus(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        System.out.println(code);
        boolean b = ExpressService.updateStatus(code);
        Message msg = new Message();
        if(b){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("取件失败，请用户更新二维码");
        }
        return GsonUtil.toJSON(msg);

    }


    @ResponseBody("/vx/findByCode.do")
    public String findByCode(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        System.out.println(code);
        Express e = ExpressService.findByCode(code);
        Message msg = new Message();
        if(e == null){
            msg.setStatus(-1);
            msg.setResult("发生错误，请刷新重试");
            return GsonUtil.toJSON(msg);
        }
        String code1 = e.getCode() == null ? "已经取件" : e.getCode();
        String inTime = DateFormatUtil.format(e.getInTime());
        String outTime = e.getOutTime() == null ? "" : DateFormatUtil.format(e.getOutTime());
        String status = e.getStatus() == 0 ? "未出库" : "已出库";
        BootStrapExpress bs = new BootStrapExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), code1, inTime, outTime, status, e.getSysPhone());

        msg.setStatus(0);
        msg.setResult("查找成功");
        msg.setData(bs);
        String s = GsonUtil.toJSON(msg);
        System.out.println(s);
        return s;
    }
}
